package com.ds.lens.data.service.dao;

import com.github.pagehelper.Page;
import com.ds.lens.data.service.dao.entity.LensUser;
import com.ds.lens.data.service.dao.sql.provider.LensUserSqlProvider;
import com.ds.lens.data.vo.in.v2.user.LensUserSearchInVO;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface LensUserDao {
    @Delete({
            "delete from lens_user",
            "where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
            "insert into lens_user (id, name, ",
            "password, email, ",
            "created_time, modified_time)",
            "values (#{id,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR}, ",
            "#{password,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR}, ",
            "#{createdTime,jdbcType=TIMESTAMP}, #{modifiedTime,jdbcType=TIMESTAMP})"
    })
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(LensUser record);

    @InsertProvider(type = LensUserSqlProvider.class, method = "insertSelective")
    int insertSelective(LensUser record);

    @Select({
            "select",
            "id, name, password, email, created_time, modified_time",
            "from lens_user",
            "where id = #{id,jdbcType=BIGINT}"
    })
    @Results(id = "resultMap", value = {
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "password", property = "password", jdbcType = JdbcType.VARCHAR),
            @Result(column = "email", property = "email", jdbcType = JdbcType.VARCHAR),
            @Result(column = "created_time", property = "createdTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "modified_time", property = "modifiedTime", jdbcType = JdbcType.TIMESTAMP)
    })
    LensUser selectByPrimaryKey(Long id);

    @UpdateProvider(type = LensUserSqlProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(LensUser record);

    @Update({
            "update lens_user",
            "set name = #{name,jdbcType=VARCHAR},",
            "password = #{password,jdbcType=VARCHAR},",
            "email = #{email,jdbcType=VARCHAR},",
            "created_time = #{createdTime,jdbcType=TIMESTAMP},",
            "modified_time = #{modifiedTime,jdbcType=TIMESTAMP}",
            "where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(LensUser record);

    @Select({
            "<script>",
            "select",
            "id, name, password, email, created_time, modified_time",
            "from lens_user",
            "where 1=1 ",
            "<if test='name != null'> and name=#{name,jdbcType=VARCHAR} </if>",
            "<if test='email != null'> and email=#{email,jdbcType=VARCHAR} </if>",
            "</script>"
    })
    @ResultMap("resultMap")
    Page<LensUser> selectByCondition(LensUser lensUser);

    @Select({
            "select",
            "id, name, password, email, created_time, modified_time",
            "from lens_user",
            "where name=#{userName,jdbcType=VARCHAR}"
    })
    @ResultMap("resultMap")
    LensUser selectLensUserByUserName(String userName);

    @Select({
            "select",
            "id, name, password, email, created_time, modified_time",
            "from lens_user",
            "where email=#{email,jdbcType=VARCHAR}"
    })
    @ResultMap("resultMap")
    LensUser selectLensUserByEmail(String email);

    @Select({
            "select",
            "count(1) > 0",
            "from lens_user",
            "where name=#{userName,jdbcType=VARCHAR}"
    })
    boolean userNameExisted(String userName);

    @Select({
            "select",
            "count(1) > 0",
            "from lens_user",
            "where email=#{email,jdbcType=VARCHAR}"
    })
    boolean emailExisted(String email);


    @Update({
            "update lens_user",
            "set password = #{password,jdbcType=VARCHAR},",
            "modified_time = now()",
            "where id = #{id,jdbcType=BIGINT}"
    })
    int updatePasswordByPrimaryKey(@Param("password") String password,
                                   @Param("id") Long id);

    /**
     * Query all lens users.
     *
     * @return List
     */
    @Select({
            "select",
            "id, name, password, email, created_time, modified_time",
            "from lens_user"
    })
    @ResultMap("resultMap")
    List<LensUser> selectAllLensUsers();

    /**
     * Paging query Lens users.
     *
     * @param inVO inVO
     * @return LensUsers
     */
    @Select({
            "<script>",
            "select",
            "id, name, password, email, created_time, modified_time",
            "from lens_user",
            "where 1 = 1 ",
            "<if test='userName != null'> and name = #{userName,jdbcType=VARCHAR} </if>",
            "<if test='email != null'> and email = #{email,jdbcType=VARCHAR} </if>",
            "</script>"
    })
    @ResultMap("resultMap")
    Page<LensUser> selectLensUsers(LensUserSearchInVO inVO);
}
